package com.atguigu.spring5.dao;

import com.atguigu.spring5.bean.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @date: 2021/12/9 11:14
 * @description:
 */

@Component
public class BookDaoImpl implements BookDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {
        String sql  ="insert into t_book values(?,?,?)";
        Object [] args = {book.getUserId(),book.getUserName(),book.getuStatus()};
        jdbcTemplate.update(sql, args);
    }

    @Override
    public void update(Book book) {
        String sql = "update  t_book set  username =?,ustatus=? where user_id =?";
        Object[] args = {book.getUserId(),book.getUserName(),book.getuStatus()};
        jdbcTemplate.update(sql,args);
    }

    @Override
    public void delete(int id) {
        String sql ="delete from t_book where user_id=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public int seleteCount() {
        String sql = "select count(*) from t_book";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public Book selectBook(int id) {
        String sql ="select *from t_book where user_id = ?";
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    @Override
    public List<Book> selectAllBook() {
        String sql = "select *from t_book";
        List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return bookList;
    }

    @Override
    public void batchAll(List<Object[]> batchArgs) {
        String sql  ="insert into t_book values(?,?,?)";
        System.out.println(sql);
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }

    @Override
    public void batchUpdate(List<Object[]> batchArgs) {
        String sql = "update  t_book set  username =?,ustatus=? where user_id =?";
        System.out.println(sql);
        jdbcTemplate.batchUpdate(sql, batchArgs);
    }

    @Override
    public void batchDelete(List<Object[]> batchArgs) {
        String sql ="delete from t_book where user_id=?";
        System.out.println(batchArgs);
        System.out.println(sql);
        jdbcTemplate.batchUpdate(sql,batchArgs);

    }


}
